﻿#region License

/*
 © 2012 Alex Kazaev
 * FolderDrive project [http://folderdrive.codeplex.com/]
 * Licensed under Ms-PL license [http://www.opensource.org/licenses/MS-PL]
 */

#endregion License

using System;
using System.Xml;

namespace FolderDrive.Core
{
  /// <summary>
  /// Gets the release information from codeplex
  /// </summary>
  public static class UpdateService
  {
    /// <summary>
    /// Determines the URL for release RSS
    /// </summary>
    private const string UpdatePath = @"http://folderdrive.codeplex.com/project/feeds/rss?ProjectRSSFeed=codeplex%3a%2f%2frelease%2ffolderdrive";

    /// <summary>
    /// Determines the current version
    /// </summary>
    private const string ThisVersion = "FolderDrive 0.04 (beta)";

    /// <summary>
    /// Gets the actual version.
    /// </summary>
    public static string ActualVersion { get; private set; }

    /// <summary>
    /// Gets the link.
    /// </summary>
    public static string Link { get; private set; }

    /// <summary>
    /// Gets the update data from codeplex.
    /// </summary>
    /// <returns>true if the update is available; otherwise false</returns>
    public static bool GetUpdateData()
    {
      try
      {
        var feed = new XmlDocument();
        feed.Load(UpdatePath);
        string text = feed.InnerXml.Substring(feed.InnerXml.IndexOf("<item>"));
        int titleStart = text.IndexOf(':', text.IndexOf("<title>")) + 1;
        int titleEnd = text.IndexOf("</title>");
        ActualVersion = text.Substring(titleStart, titleEnd - titleStart);
        ActualVersion = ActualVersion.Substring(0, ActualVersion.LastIndexOf('(') - 1).Trim();
        int linkStart = text.IndexOf("<link>") + 6;
        int linkEnd = text.IndexOf("</link>");
        Link = text.Substring(linkStart, linkEnd - linkStart);
        return ThisVersion != ActualVersion;
      }
      catch (Exception)
      {
        return false;
      }
    }
  }
}